package com.example.expandablelistview.listview;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.TextView;
import android.widget.Toast;

import com.example.expandablelistview.R;

public class ExpandableListViewEx extends Activity implements
		OnGroupClickListener, OnChildClickListener {
	private ExpandableListView listView;
    private String[] groups = { "Number", "Alphabet" };
    private String[][] childs = { 
    		{ "1", "2", "3", "4", "5" },
            { "A", "B", "C", "D", "E" } 
    		};

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.expandablelistviewex);

        ExpandableListAdapter listAdapter = new MyExpandableListAdapter();

        listView = (ExpandableListView) findViewById(R.id.expandablelist);
        listView.setAdapter(listAdapter);
        listView.setOnGroupClickListener(this);
        listView.setOnChildClickListener(this);
    }

	@Override
	public boolean onChildClick(ExpandableListView parent, View v,
			int groupPosition, int childPosition, long id) {
		Toast.makeText(this, "Child : " + childs[groupPosition][childPosition], Toast.LENGTH_SHORT).show();
		return true;
	}

	@Override
	public boolean onGroupClick(ExpandableListView parent, View v,
			int groupPosition, long id) {
		Toast.makeText(this, "Group : " + groups[groupPosition], Toast.LENGTH_SHORT).show();
		return false;
	}
	
	public class MyExpandableListAdapter extends BaseExpandableListAdapter {

		@Override
		public int getGroupCount() {
			return groups.length;
		}

		@Override
		public int getChildrenCount(int groupPosition) {
			return childs[groupPosition].length;
		}

		@Override
		public Object getGroup(int groupPosition) {
			return groups[groupPosition];
		}

		@Override
		public Object getChild(int groupPosition, int childPosition) {
			return childs[groupPosition][childPosition];
		}

		@Override
		public long getGroupId(int groupPosition) {
			return groupPosition;
		}

		@Override
		public long getChildId(int groupPosition, int childPosition) {
			return childPosition;
		}

		@Override
		public boolean hasStableIds() {
			//그룹과 차일드가 반환하는 아이디가 항상 일관된 아이디라면 true
			//배열의 인덱스는 항상고정이므로 true를 반환
			return true;
		}

        public TextView getGenericView() {
        	//인플레이션을 사용해서 하는 것이 더 나은 방법
            AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, 64);

            TextView textView = new TextView(ExpandableListViewEx.this);
            textView.setLayoutParams(lp);
            textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.START);
            textView.setPadding(36, 0, 0, 0);
            return textView;
        }

        
		@Override
		public View getGroupView(int groupPosition, boolean isExpanded,
				View convertView, ViewGroup parent) {
			TextView textView = getGenericView();//인플레이션을 사용해서 하는 것이 더 나은 방법
            textView.setText(getGroup(groupPosition).toString());

            // 그룹 표시 아이콘 존재 여부
            // listView.setGroupIndicator(null);

            return textView;

		}

		@Override
		public View getChildView(int groupPosition, int childPosition,
				boolean isLastChild, View convertView, ViewGroup parent) {
			TextView textView = getGenericView();
            textView.setText(getChild(groupPosition, childPosition).toString());
            return textView;
		}

		@Override
		public boolean isChildSelectable(int groupPosition, int childPosition) {
			return true;
		}
		
	}

}
